#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
#include <iostream>

using namespace std;

static void pr_times( clock_t, struct tms *, struct tms * );
static void do_cmd( char * );

int main( int argc, char *argv[] )
{
	setbuf( stdout, NULL ); // setbuf ??
	for( int i = 1; i < argc; i++ ) {
		do_cmd( argv[i] );
	}
	exit( 0 );
}

static void do_cmd( char *cmd )
{
	struct tms tmsstart, tmsend;
	clock_t start, end;
	int status;

	printf( "\ncommnad: %s", cmd );
	if( (start = times( &tmsstart )) == -1 ) // starting value
		perror( "times error" );
	sleep( 5 );
	if( (status = system( cmd )) < 0 )
		perror( "system() error" );
	if( (end = times( &tmsend )) == -1 )
		perror( "times error" );
	pr_times( end - start, &tmsstart, &tmsend );
	// pr_exit( status );
}

static void pr_times( clock_t real, struct tms *tmsstart, struct tms *tmsend )
{
	static long clktck = 0;

	if( 0 == clktck )   // fetch clock ticker per second first time
		if( (clktck = sysconf( _SC_CLK_TCK )) < 0 )
			perror( "sysconf error" );
	cout << clktck << endl;
	printf( "\treal: %8.3f\n", real / (double)clktck );
	printf( "\tuser: %8.3f\n", (tmsend->tms_utime - tmsstart->tms_utime) / (double)clktck );
	printf( "\tsys: %8.3f\n", (tmsend->tms_stime - tmsstart->tms_stime) / (double)clktck );
	printf( "\tchild user: %8.3f\n", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double)clktck );
	printf( "\tchild sys: %8.3f\n", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double)clktck );
}
